﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class QuanLyNguoiDung : UserControl
    {
        public QuanLyNguoiDung()
        {
            InitializeComponent();
        }
        private void loadData()
        {
            dgvUser.Rows.Clear();

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var users = from s in dc.NguoiDungs where s.deleted == false select s;
            int count = 1;
            foreach (_DB.NguoiDung u in users)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvUser);
                row.Cells[0].Value = count++;
                //row.Cells[1].Value = u.Username;
                row.Cells[1].Value = u.MaNguoiDung;
                row.Cells[2].Value = u.Ten;
                if (u.PhongBan != null)
                    row.Cells[3].Value = u.PhongBan.TenPhongBan;
                if (u.NhomNguoiDung != null)
                    row.Cells[4].Value = u.NhomNguoiDung.TenNhom;
                dgvUser.Rows.Add(row);
            }
        }

        private void QuanLyNguoiDung_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OtherForms.ThemUserForm addUser = new OtherForms.ThemUserForm();
            addUser.ShowDialog();
            loadData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvUser.SelectedRows.Count == 0)
            {
                return;
            }
            String id = (String)dgvUser.SelectedRows[0].Cells[1].Value as string;
            _DB.NguoiDung u = _DB.NguoiDungDB.selectID(id);
            //MessageBox.Show("asdsa" + u.MaNguoiDung);
            if (u != null)
            {
                OtherForms.ThemUserForm addUser = new OtherForms.ThemUserForm(u, 1);
                addUser.ShowDialog();
                loadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvUser.SelectedRows.Count == 0)
                return;
            if (MessageBox.Show("Chắc chắn xóa?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
                return;
            foreach (DataGridViewRow row in dgvUser.SelectedRows)
            {
                String id = (String)row.Cells[1].Value as string;
                _DB.NguoiDungDB.delete(id);
            }
            loadData();
        }

        private void dgvUser_DoubleClick(object sender, EventArgs e)
        {
            if (dgvUser.SelectedRows.Count == 0)
                return;

            String id = dgvUser.SelectedRows[0].Cells[2].Value as string;
            _DB.NguoiDung u = _DB.NguoiDungDB.selectID(id);
            if (u != null)
            {
                OtherForms.ThemUserForm addUser = new OtherForms.ThemUserForm(u, 0);
                addUser.ShowDialog();
            }
        
        }

    }
}
